package com.sinodata.bsm.cicp.finder.ha;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * 
 * <p>
 * Description: 双机进程自发现程序， 发现 进程名
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 10:18:56   liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class HAProcessFinder extends Finder {

    private static final Logger logger = Logger.getLogger(HAProcessFinder.class);

    public HAProcessFinder() {

    }

    /**
     * 发现程序
     * 
     * @param pResValue  父资源的信息
     * @param pattrVals  父资源属性
     * @param attributes 资源自带的属性
     * @return 资源列表
     */
    /* public List<List<Serializable>> find(ResValue pResValue, ResAttributeValValue[] pattrVals, ResAttributeValue[] attributes) {
         String shell = "lssrc -g cluster";
         BufferedReader reader = null;
         try {
             reader = CollectorUtil.getReader(shell, host);
             return this.parseReader(reader, pattrVals, attributes);
         } catch (IOException e) {
             logger.error("Failed to auto find HAProcess ", e);
         } finally {
             if (reader != null) {
                 try {
                     reader.close();
                 } catch (IOException e) {
                     logger.error("Failed to close IO resource ", e);
                 }
             }
         }
         return new ArrayList<List<Serializable>>();
     }*/

    /**
     * 解析命令响应结果
     * 
     * @param reader 命令响应结果
     * @param pattrVals  接口父资源 节点的属性信息
     * @param attributes 资源自带的属性
     * @return 资源列表
     * @throws IOException
     */
    /*private List<List<Serializable>> parseReader(BufferedReader reader, ResAttributeValValue[] pattrVals, ResAttributeValue[] attributes) throws IOException {
        List<List<Serializable>> tlqResources = null;
        // 匹配标题行
        Pattern titlePatn = Pattern.compile(".*Subsystem.*");
        // 匹配目标数据
        Pattern dataPatn = Pattern.compile("\\s*(\\S*).*");
        String tmp = null;
        List<Serializable> resAndAttr = null;
        Matcher titleMach = null;
        Matcher dataMach = null;
        tlqResources = new ArrayList<List<Serializable>>();
        while ((tmp = reader.readLine()) != null) {
            titleMach = titlePatn.matcher(tmp);
            dataMach = dataPatn.matcher(tmp);
            if (titleMach.matches()) {
                continue;
            } else if (dataMach.matches()) {
                resAndAttr = new ArrayList<Serializable>();
                String busCode = dataMach.group(1);
                resAndAttr.add(findRes(busCode));
                resAndAttr.add(findAttr(attributes, busCode));
                tlqResources.add(resAndAttr);
            }
        }
        return tlqResources;
    }*/

    /**
     * 创建ResValue
     * 
     * @param busCode 资源名称
     * @return ResValue
     */
    /* private ResValue findRes(String busCode) {
         ResValue resValue = new ResValue();
         resValue.setIp(host);
         resValue.setName(busCode);
         resValue.setInternalId(busCode);
         resValue.setDescript("HAProcess Name");
         return resValue;
     }*/

    /**
     * 创建资源的属性
     * 
     * @param attributes
     * @param i  属性数组的角标
     * @param busCode 设置给属性的值
     * @return
     */
    /*private ResAttributeValValue findAttr(ResAttributeValue[] attributes, String busCode) {
        ResAttributeValValue nameAttr = new ResAttributeValValue();
        nameAttr.setResAttributeId(attributes[0].getId());// name
        nameAttr.setAttrValue(busCode);
        return nameAttr;
    }*/

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        String shell = "lssrc -g cluster";
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, shell, 0, 0);
            return parseReader(reader);
        } catch (IOException e) {
            logger.error("Failed to auto find HAProcess ", e);
            return new ArrayList<String[]>();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("Failed to close IO resource ", e);
                }
            }
        }
    }

    private List<String[]> parseReader(BufferedReader reader) throws IOException {
        // 匹配标题行
        Pattern titlePatn = Pattern.compile(".*Subsystem.*");
        // 匹配目标数据
        Pattern dataPatn = Pattern.compile("\\s*(\\S*).*");
        String tmp = null;
        List<String[]> list = null;
        Matcher titleMach = null;
        Matcher dataMach = null;
        while ((tmp = reader.readLine()) != null) {
            titleMach = titlePatn.matcher(tmp);
            dataMach = dataPatn.matcher(tmp);
            if (titleMach.matches()) {
                continue;
            } else if (dataMach.matches()) {
                String busCode = dataMach.group(1);
                String[] info = new String[4];
                info[0] = busCode;
                info[1] = busCode;
                info[2] = "HAProcess Name";
                info[3] = busCode; //HAProcess Name
            }
        }
        return list;
    }
}